package com.ddx.controller;

import com.ddx.common.exception.ResultModel;
import com.ddx.service.AdvertService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2019-01-26
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/app/advert/")
public class AdvertController {

    private static final Logger logger = LoggerFactory.getLogger(AdvertController.class);

    @Autowired
    AdvertService advertService;

    @RequestMapping(value = "getAdvertList")
    public ResultModel getAdvertList(HttpServletRequest request) {
        try {
            Integer userId = Integer.parseInt(request.getHeader("userId"));
            return advertService.getAdvertListByUserId(userId);
        } catch (Exception e) {
            logger.error("getAdvertList error, ", e);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }

    @RequestMapping(value = "getAdvertById")
    public ResultModel getAdvertById(HttpServletRequest request, Map<String, Object> map) {
        try {
            int advertId = Integer.parseInt(map.get("advertId").toString());
            return advertService.getAdvertById(advertId);
        } catch (Exception e) {
            logger.error("getAdvertList error, ", e);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }
}
